package com.bailiban.aigou.admin.action;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.bailiban.aigou.admin.common.BaseAction;
import com.bailiban.aigou.admin.common.reference;
import com.bailiban.aigou.admin.entity.BackstageUser;
import com.bailiban.aigou.admin.service.IUserService;
import com.bailiban.aigou.admin.utils.PageModel;

@Controller
@Scope("prototype")
@Namespace("/admin/user")
@Results({
    @Result(name = "error", location = "/WEB-INF/jsp/aigou/500.jsp"),
	@Result(name = "login", location = "/WEB-INF/jsp/aigou/admin/login/login.jsp"),
	@Result(name = "close", location = "/WEB-INF/jsp/aigou/success.jsp")
})

public class UserAdminAction extends BaseAction{
	
	
	/**dssd
	 * 类名：UserAdminAction
	 * 类型：@Controller层  action
	 * 功能描述：用户管理 用户维护
	 * **/
	
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private IUserService userService;
	/**
	 * 用户分页数据
	 * @return
	 */
	@Action(value="list",results={ @Result( name=SUCCESS ,location="/WEB-INF/jsp/aigou/admin/user/list.jsp")})
	public String list(){
		System.out.println("用户管理");
		HttpServletRequest request = ServletActionContext.getRequest();
		super.setPageNo(request.getParameter("pageNo"));
		super.setPageSize(request.getParameter("pageSize"));
		
		PageModel<BackstageUser> pageModel = userService.getAdminUser(super.getPageNo(), super.getPageSize());
		request.setAttribute("userPages", pageModel);
		return SUCCESS;
		
		
	}
	/**
	 * 去用户添加界面
	 * @return
	 */
	@Action(value = "addPage", results = {
			@Result(name = SUCCESS, location = "/WEB-INF/jsp/aigou/admin/user/add.jsp")	
	})
	public String addPage(){
		return SUCCESS;
	}
	/**
	 * 添加用户
	 * @return
	 */
	@Action(value = "add")
	public String add(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String userName = request.getParameter("userName");
		String password = request.getParameter("password");
		
		BackstageUser user = new BackstageUser();
		user.setUsername(userName);
		user.setPassword(password);
		userService.save(user);
		
		request.setAttribute("url", "admin/user/list");
		return "close";
	}
	/**
	 * 去修改页面
	 * @return
	 */
	@Action(value = "updatePage", results = {
			@Result(name = SUCCESS, location = "/WEB-INF/jsp/aigou/admin/user/update.jsp")	
	})
	public String updatePage(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("id");
		
		BackstageUser user = userService.getIdByUser(id);
		request.setAttribute("user", user);
		return SUCCESS;
	}
	 //修改
	   @Action(value = "update",results={@Result(name="success",location= "/WEB-INF/jsp/aigou/admin/user/list.jsp")})
	   public String update(){
		  HttpServletRequest request = ServletActionContext.getRequest();
		  String id = request.getParameter("id");
		  String username = request.getParameter("username");
		  String password = request.getParameter("password");
		  BackstageUser user = userService.getIdByUser(id);
		 user.setUsername(username);
		 user.setPassword(password);
		 userService.updateUser(user);
		 request.setAttribute("url", "/admin/user/list");
		return "close";  
	   }
	   //删除
	   @Action(value = "delete",results={@Result(name="success",location= "/WEB-INF/jsp/aigou/admin/user/list.jsp")})
	   public String delete(){
		   HttpServletRequest request = ServletActionContext.getRequest();
		   String id = request.getParameter("id");
		   userService.deleteuser(id);
		   request.setAttribute("url", "/admin/user/list");
		return "close";  
	   }
	   /**
	    * 去修改当前登录用户密码页面
	    * @return
	    */
	   @Action(value = "changePasswordPage", results = {
				@Result(name = SUCCESS, location = "/WEB-INF/jsp/aigou/admin/user/changePassword.jsp")	
		})
	  public String changePasswordPage(){
		   HttpServletRequest request = ServletActionContext.getRequest();
			String username = reference._adminuUername;
			
			BackstageUser user = userService.getNameUser(username);
			request.setAttribute("user", user);
			return SUCCESS;
		
	}
	   /**
	    * 修改当前登录用户密码
	    * @return
	    */
	   @Action(value = "changePassword")
	   public String  changePassword(){
		HttpServletRequest request = ServletActionContext.getRequest();
		
		request.setAttribute("url", "/admin/user/list");
		return "close";
		   
	   }
}
